package com.west.common.widget.banner;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.west.common.common.R;
import com.west.common.imageloader.GlideImageLoader;
import com.west.common.imageloader.ImageConfigImpl;
import com.west.common.uitils.StringUtils;


public class BannerItem extends RelativeLayout implements View.OnClickListener {
    private Banner mBanner;
    private ImageView iv_banner;

    public BannerItem(Context context) {
        super(context, null);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.view_news_banner, this, true);
        RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relative);
        iv_banner = (ImageView) findViewById(R.id.iv_banner);
        setOnClickListener(this);
    }

    public void initData(Banner banner) {
        this.mBanner = banner;
        String url = null;
        if (!TextUtils.isEmpty(banner.getPicturepath())) {
            url = StringUtils.getPhoto(banner.getPicturepath());
            GlideImageLoader.getImageLoader().loadImage(getContext(), ImageConfigImpl.builder().placeholder(R.color.default_color)
                    .imageRadius(banner.isRadius() ? 16 : 0)
                    .errorHolder(R.color.default_color)
                    .url(url)
                    .isDont()
                    .imageView(iv_banner)
                    .build());
        } else if (banner.getPic() > 0) {
            GlideImageLoader.getImageLoader().loadImage(getContext(), ImageConfigImpl.builder().placeholder(R.color.default_color)
                    .imageRadius(banner.isRadius() ? 16 : 0)
                    .errorHolder(R.color.default_color)
                    .uid(banner.getPic())
                    .isDont()
                    .imageView(iv_banner).build());
        }

    }

    @Override
    public void onClick(View v) {
        if (mBanner == null || mListener == null) {
            return;
        }
        if(mListener != null){
            mListener.onBannItemClick(mBanner);
        }

    }

    public String getTitle() {
        return "";
    }

    private OnBannerItemClickListener mListener;

    public void setmListener(OnBannerItemClickListener mListener) {
        this.mListener = mListener;
    }

    public void setmBanner(Banner mBanner) {
        this.mBanner = mBanner;
    }

    public interface OnBannerItemClickListener {
        void onBannItemClick(Banner banner);
    }

}
